package com.housekeeping.housekeeping.serviceItem.controller;

import com.alibaba.fastjson.JSONObject;
import com.housekeeping.housekeeping.serviceItem.entity.ServiceItemPage;
import com.housekeeping.housekeeping.serviceItem.service.ServiceItemService;
import com.housekeeping.housekeeping.serviceItem.service.impl.ServiceItemServiceImpl;
import com.housekeeping.housekeeping.serviceItem.utils.RequestUtils;
import com.housekeeping.housekeeping.serviceItem.utils.ResponseUtils;
import com.housekeeping.housekeeping.serviceItem.utils.StatUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ServiceItem/update")
public class ServiceItemUpdateController extends HttpServlet {
    private ServiceItemService service=new ServiceItemServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String details = req.getParameter("itemDetails");
            String itemPageStr = req.getParameter("itemPage");
            String aid= RequestUtils.verify(req);
            //非空判断
            if (details==null||itemPageStr==null||aid==null) { ResponseUtils.fail(resp, StatUtil.PARAMS_VALID); }
            else {
                ServiceItemPage itemPage = JSONObject.parseObject(itemPageStr, ServiceItemPage.class);
                int res= service.update(aid,itemPage,details);
                if (res==1){ResponseUtils.success(resp,StatUtil.SUCCESS);}
                else {ResponseUtils.fail(resp,StatUtil.SERVER_BUST);}
            }
        }catch (Exception e)
        {
            e.printStackTrace();
            ResponseUtils.fail(resp,StatUtil.INTERNET_ERR);
        }
    }
}
